package lamda;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author LoneRanger
 * @version 1.0
 */
public class StreamTest {

	@Test
	public void sum0() {
		List<Phone> phones = new ArrayList<>();
		phones.add(new Phone(10, "A"));
		phones.add(new Phone(11, "B"));
		phones.add(new Phone(12, "C"));
		Integer reduce = phones.stream().map(Phone::getPrice).reduce(0, Integer::sum);
		System.out.println(reduce);
	}

	@Test
	public void sum1() {
		List<Phone> phones1 = new ArrayList<>();
		phones1.add(new Phone(10, "A"));
		phones1.add(new Phone(11, "B"));
		phones1.add(new Phone(12, "C"));

		List<Phone> phones2 = new ArrayList<>();
		phones1.add(new Phone(3, "E"));
		phones1.add(new Phone(6, "D"));
		phones1.add(new Phone(9, "F"));


		List<Bag> bags = new ArrayList<>();
		bags.add(new Bag(phones1));
		bags.add(new Bag(phones2));

		System.out.println(bags.stream().mapToInt(item -> item.getPhones().size()).sum());


	}

	/**
	 * 给一个Phone list 中的type去重
	 */
	@Test
	public void propertiesToList() {
		List<Phone> phones = new ArrayList<>();
		phones.add(new Phone(10, "A"));
		phones.add(new Phone(11, "B"));
		phones.add(new Phone(12, "C"));
		phones.add(new Phone(14, "C"));
		phones.add(new Phone(15, "A"));

		List<String> collect =
				phones.stream().map(Phone::getType).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());

		System.out.println(collect);
	}

	@Test
	public void group() {
		List<Phone> phones = new ArrayList<>();
		phones.add(new Phone(10, "A"));
		phones.add(new Phone(11, "B"));
		phones.add(new Phone(12, "C"));
		phones.add(new Phone(14, "C"));
		phones.add(new Phone(15, "A"));

		Map<String, List<Phone>> collect =
				phones.stream().collect(Collectors.groupingBy(Phone::getType));
		System.out.println(JSON.toJSONString(collect));
	}

	@Test
	public void listToMap() {
		List<Phone> phones = new ArrayList<>();
		phones.add(new Phone(10, "A"));
		phones.add(new Phone(11, "B"));
		phones.add(new Phone(12, "C"));
		phones.add(new Phone(14, "C"));
		phones.add(new Phone(15, "A"));

		Map<Integer, Phone> collect = phones.stream().collect(Collectors.toMap(Phone::getPrice
				, Function.identity(), (key1, key2) -> key2));
		System.out.println(JSON.toJSONString(collect));
	}

	@Test
	public void listToMap2() {
		List<Phone> phones = new ArrayList<>();
		phones.add(new Phone(10, "A"));
		phones.add(new Phone(11, "B"));
		phones.add(new Phone(12, "C"));
		phones.add(new Phone(14, "C"));
		phones.add(new Phone(15, "A"));
		phones.add(new Phone(15, "D"));

		Map<Integer, String> collect = phones.stream().collect(Collectors.toMap(Phone::getPrice
				, Phone::getType, (key1, key2) -> key1));
		System.out.println(JSON.toJSONString(collect));
	}

	@Test
	public void join() {
		List<String> strings = Arrays.asList(new String[]{"aaabbbb", "cccddd", "iiiooo"});
	}

	@Test
	public void testSorted() {
		List<Phone> phones = new ArrayList<>();
		phones.add(new Phone(15, "A"));
		phones.add(new Phone(11, "B"));
		phones.add(new Phone(10, "A"));
		phones.add(new Phone(12, "C"));
		phones.add(new Phone(14, "C"));

		List<Phone> collect =
				phones.stream().sorted(Comparator.comparing(Phone::getPrice).reversed()).collect(Collectors.toList());

		System.out.println(JSON.toJSONString(collect));
	}

	public static void main(String[] args) {
		new HashSet<>(new ArrayList<>());
	}


}

class Phone {

	int price;

	String type;

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getType() {
		return type;
	}

	public Phone(int price, String type) {
		this.price = price;
		this.type = type;
	}

	public void setType(String type) {
		this.type = type;
	}
}

class Bag {
	List<Phone> phones = new ArrayList<>();

	public Bag(List<Phone> phones) {
		this.phones = phones;
	}

	public List<Phone> getPhones() {
		return phones;
	}

	public void setPhones(List<Phone> phones) {
		this.phones = phones;
	}
}
